<template>
  <div>
    <!-- 面包屑   -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/layout' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a>栏目管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>栏目列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片    -->
    <el-card class="box-card">
      <el-button type="primary" @click="open">新增</el-button>
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        height="calc( 100vh - 210px )"
      >
        <el-table-column label="序号" prop="typeId" />
        <el-table-column prop="type" label="标题" />
        <el-table-column prop="remarks" label="备注" />

        <el-table-column label="操作" width="180">
          <template v-slot="{ row }">
            <!-- <el-button size="mini" @click="handleEdit(row.typeId)"
              >编辑</el-button
            > -->
            <el-button size="mini" type="danger" @click="handleDel(row.typeId)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog
      title="新增栏目"
      :visible="dialogVisible"
      width="80%"
      @close="dialogVisible = false"
    >
      <el-card class="box-card">
        <el-form label-width="80px">
          <el-form-item label="备注">
            <el-input v-model="formData.remarks"></el-input>
          </el-form-item>
          <el-form-item label="栏目">
            <el-input v-model="formData.type"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">立即创建</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </el-dialog>
  </div>
</template>

<script>
import { getType, delType, adddType } from '@/api';
export default {
  data() {
    return {
      tableData: [],
      dialogVisible: false,
      formData: {
        remarks: '',
        type: '',
      },
    };
  },
  methods: {
    async handleDel(typeId) {
      const res = await delType({ typeId });
      if (res.data.code === 1) {
        this.getList();
        this.$message.success(res.data.msg);
      } else {
        this.$message.warning(res.data.msg);
      }
    },
    async getList() {
      const { data } = await getType();
      this.tableData = data.list;
    },
    open() {
      this.dialogVisible = true;
    },
    async onSubmit() {
      if (this.formData.remarks.length > 0 && this.formData.type.length > 0) {
        const res = await adddType(this.formData);
        if (res.data.code === 1) {
          this.formData = {
            remarks: '',
            type: '',
          };
          this.$message.success(res.data.msg);
          await this.getList();
          this.dialogVisible = false;
        } else {
          this.$message.warning(res.data.msg);
        }
      } else {
        this.$message.warning('新增栏目必填项哦!');
      }
    },
  },
  created() {
    this.getList();
  },
};
</script>

<style lang="less" scoped>
.el-card {
  margin-top: 20px;
}
</style>
